package com.cn.hibernate.service.impl;



import com.cn.hibernate.dao.IUserDao;
import com.cn.hibernate.model.User;
import com.cn.hibernate.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class UserService implements IUserService {

	@Autowired
	private IUserDao userDao;
	
	@Transactional(propagation=Propagation.REQUIRED)
	public void save(User user) {
		userDao.save(user);
		//throw new RuntimeException();
	}
	
	//如果用了下面的nested传播，那么在action中执行保存再执行删除，保存依然执行成功。是因为nested是一个新事物,还有notsupported,never,require new等等
	//好像需要另一个transactional，用到再说
	@Transactional(propagation=Propagation.NESTED)
	public void delete(User user){
		userDao.delete(user);
		throw new RuntimeException();
	}
	
	public User get(Long id){
		return userDao.get(id);
	}
	
	public List<User> findAll(){
		return userDao.findAll();
	}
	public void test(){
		userDao.test();
	}
	
	public void testBatch(){
		userDao.testBatch();
	}
}
